#include"TcpServer.hpp"
#include"Http.hpp"
#include<memory>

void Usage(std::string proc)
{
    std::cerr << "Usage: " << proc << " serverport" << std::endl;
}

std::string TestHttp(std::string& requeststr)
{
    std::cout << "##############################" << std::endl;
    std::cout << requeststr << std::endl;
    std::cout << "##############################" << std::endl;

    // return "hello";
    //构建简单的应答
    std::string response = "HTTP/1.1 200 OK\r\n\r\n";

    response += "<!DOCTYPE html><html><head><title>Hello World</title></head><body><h1>Hello World!</h1></body></html>";
    return response;
}

//./server serverport
int main(int argc , char * argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        return 0;
    }

    EnableConsoleLogStrategy();//指定刷新策略
    //守护进程，可以将日志信息保存在文件中
    // EnableFileLogStrategy();

    uint16_t port = std::stoi(argv[1]);

    //Http协议
    std::unique_ptr<Http> http = std::make_unique<Http>();

    std::unique_ptr<TcpServer> svr = std::make_unique<TcpServer>(port,
    [&http](std::string&requeststr)->std::string{
        return http->HandlerRequest(requeststr);
    });

    svr->Run();
    return 0;
}